//导入模型
const { Course_schedule } = require('../../models/Course outline/course_schedule')
const { unit_Knowledge } = require('../../models/Course outline/unit_Knowledge')

//查找
exports.getCourseScheduleData = async (req, res) => {
  const { t_user_and_a_c_s_id } = req.query;

  try {
    const classes = await Course_schedule.findAll({
      where: {
        t_user_and_a_c_s_id: t_user_and_a_c_s_id,
      },
    });

    if (classes.length === 0) {
      return res.json({
        'code': 203,
        'msg': '没有对应数据'
      });
    }

    res.json({
      'code': 200,
      'msg': '查询成功',
      'data': classes
    });
  } catch (error) {
    console.error('查询数据失败:', error);
    res.json({
      'code': 500,
      'msg': '查询失败'
    });
  }
};

// 添加
exports.ADD_Course_Schedule = async (req, res) => {
  const { content, all_time, theory_time, practice_time, t_user_and_a_c_s_id } = req.body;

  try {
    // 第一步：在unit_Knowledge表中找到与content和t_user_and_a_c_s_id相关的数据，并获取id
    const unitKnowledge = await unit_Knowledge.findOne({ where: { content, t_user_and_a_c_s_id } });

    if (!unitKnowledge) {
      res.json({
        code: 404,
        msg: '未找到相关的unit_Knowledge数据'
      });
      return;
    }

    const unitKnowledgeId = unitKnowledge.id;

    // 第二步：将unit_knowledge_id、all_time、theory_time、practice_time和t_user_and_a_c_s_id插入到Course_schedule表中
    await Course_schedule.create({ unit_knowledge_id: unitKnowledgeId, all_time, theory_time, practice_time, t_user_and_a_c_s_id });

    res.json({
      code: 200,
      msg: '数据已插入'
    });
  } catch (error) {
    console.error('操作失败:', error);
    res.json({
      code: 500,
      msg: '操作失败'
    });
  }
};